package org.example;

import org.example.entity.SmtpConfig;
import org.example.entity.User;
import org.example.service.UserService;
import org.example.service.Validators;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*;

import java.time.ZoneId;

@Configuration
@ComponentScan
@PropertySource("app.properties")
@EnableAspectJAutoProxy
public class AppConfig {

    @Value("${app.zone:Z}")
    String zoneId;

    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);

        UserService userService = context.getBean(UserService.class);
        // 获取userService实例类型
        System.out.println("userService.getClass() = " + userService.getClass());
        User user = userService.login("bob@example.com", "password");
        System.out.println("user.name = " + user.getName());

        Validators validators = context.getBean(Validators.class);
        validators.validate("bob@example.com", "password","wangweiliang");

        SmtpConfig smtpConfig = context.getBean(SmtpConfig.class);
        System.out.println("smtpConfig.getHost() = " + smtpConfig.getHost());
        System.out.println("smtpConfig.getPort() = " + smtpConfig.getPort());
    }

    @Bean("z")
    ZoneId createZoneId(){
        return ZoneId.of("Z");
    }

    @Bean("utc8")
    ZoneId createZoneOfUTC8(){
        return ZoneId.of("UTC+08:00");
    }

    @Bean("zdy")
    @Profile("!test")
    ZoneId createZoneIdByProperty(){
        return ZoneId.of(zoneId);
    }

    @Bean("zdy")
    @Profile({"test","master"})
    ZoneId createZoneIdByProfile(){
        return ZoneId.of("America/New_York");
    }
}